import { Component, OnInit } from '@angular/core';
import { Device, DeviceStatus } from '../../models/device.model';
import { DeviceService } from '../../services/device.service';

@Component({
  selector: 'app-device-list',
  templateUrl: './device-list.component.html',
  styleUrls: ['./device-list.component.scss']
})
export class DeviceListComponent implements OnInit {
  devices: Device[] = [];
  loading = false;

  constructor(private deviceService: DeviceService) { }

  ngOnInit(): void {
    this.loadDevices();
  }

  loadDevices(): void {
    this.loading = true;
    this.deviceService.getAllDevices().subscribe({
      next: (devices) => {
        this.devices = devices;
        this.loading = false;
      },
      error: (error) => {
        console.error('Error loading devices:', error);
        this.loading = false;
      }
    });
  }

  getStatusText(status: DeviceStatus): string {
    switch (status) {
      case DeviceStatus.Available: return '可用';
      case DeviceStatus.InUse: return '使用中';
      case DeviceStatus.UnderRepair: return '维修中';
      case DeviceStatus.Retired: return '已退役';
      default: return '未知';
    }
  }

  getStatusClass(status: DeviceStatus): string {
    switch (status) {
      case DeviceStatus.Available: return 'success';
      case DeviceStatus.InUse: return 'info';
      case DeviceStatus.UnderRepair: return 'warning';
      case DeviceStatus.Retired: return 'danger';
      default: return 'secondary';
    }
  }
} 